﻿using System;
using System.Collections.Generic;
using System.Text;
using Business.Entities;
using System.Data;
using System.Data.SqlClient;

namespace Data.Database
{
    public class ComisionAdapter:Adapter
    {
        public List<Comision> GetAll() //Correcto
        {
            try
            {
                List<Comision> ListaComisiones = new List<Comision>();
                this.OpenConnection();
                SqlCommand cmdGet = new SqlCommand("select * from comisiones c inner join planes p on " +
                    "c.id_plan=p.id_plan order by c.id_plan, c.anio_especialidad, c.desc_comision", sqlConn);
                SqlDataReader comReader = cmdGet.ExecuteReader();
                while (comReader.Read())
                {
                    Comision comActual = new Comision();
                    comActual.AnioEspecialidad = (int)comReader["anio_especialidad"];
                    comActual.Descripcion = comReader["desc_comision"].ToString();
                    comActual.ID = (int)comReader["id_comision"];
                    comActual.IDPlan = (int)comReader["id_plan"];
                    comActual.DescripcionPlan = (string)comReader["desc_plan"];
                    ListaComisiones.Add(comActual);
                }
                comReader.Close();
                return ListaComisiones;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
            }
        }

        public Comision GetOne(int ID) //Correcto
        {
            try
            {
                Comision ComAct = new Comision();
                this.OpenConnection();
                SqlCommand cmdGetOne = new SqlCommand("select * from comisiones where id_comision = @idcom", this.sqlConn);
                cmdGetOne.Parameters.Add("@idcom", typeof(int)).Value = ID;
                SqlDataReader reader = cmdGetOne.ExecuteReader();
                while (reader.Read())
                {
                    ComAct.IDPlan = (int)reader["id_plan"];
                    ComAct.ID = (int)reader["id_comision"];
                    ComAct.Descripcion = reader["desc_comision"].ToString();
                    ComAct.AnioEspecialidad = (int)reader["anio_especialidad"];
                }
                reader.Close();
                return ComAct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
            }
        }

        public void Delete(Comision com) //Correcto
        {
            try
            {
                this.OpenConnection();
                SqlCommand cmdDelete = new SqlCommand("delete from comisiones where id_comision=@id", sqlConn);
                cmdDelete.Parameters.Add("@id", typeof(int)).Value = com.ID;
                cmdDelete.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
            }
        }

        public void Save(Comision com) //Correcto
        {
            try
            {
                this.OpenConnection();
                if (com.State == Comision.States.New)
                {
                    SqlCommand cmdSave = new SqlCommand("insert into comisiones(desc_comision, anio_especialidad, id_plan) " +
                        "values(@desc, @anio, @idplan)", sqlConn);
                    cmdSave.Parameters.Add("@desc", typeof(int)).Value = com.Descripcion;
                    cmdSave.Parameters.Add("@anio", typeof(string)).Value = com.AnioEspecialidad;
                    cmdSave.Parameters.Add("@idplan", typeof(int)).Value = com.IDPlan;
                    cmdSave.ExecuteNonQuery();
                }
                else if (com.State == Comision.States.Modified)
                {
                    SqlCommand cmdModifica = new SqlCommand("update comisiones set desc_comision=@desc, anio_especialidad=@anio, id_plan=@plan " +
                        "where id_comision=@idcom", sqlConn);
                    cmdModifica.Parameters.Add("@idcom", typeof(int)).Value = com.ID;
                    cmdModifica.Parameters.Add("@anio", typeof(int)).Value = com.AnioEspecialidad;
                    cmdModifica.Parameters.Add("@plan", typeof(int)).Value = com.IDPlan;
                    cmdModifica.Parameters.Add("@desc", typeof(string)).Value = com.Descripcion;
                    cmdModifica.ExecuteNonQuery();
                }
                else if (com.State == Comision.States.Unmodified)
                {

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
            }
        }
    }
}
